import apis from "../server/ReportApi";

const { getTaskDetail, getProcessInstanceDetail, getTaskTotal, getProcessInstanceTotal } = apis;

const initData = {
  TaskDetailCount: 0,
  InstanceDetailCount: 0,
  TaskTotalCount: 0,
  InstanceTotalCount: 0,
  TaskDetailData: [],
  ProcessInstanceDetailData: [],
  TaskTotalData: [],
  ProcessInstanceTotalData: [],
};

export default {
  namespace: "Report",
  state: {
    ...initData,
  },

  effects: {
    * getTaskDetail({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(getTaskDetail, payload);
      yield put({ type: "set/TaskDetailData", payload: datas, TaskDetailCount: totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * getProcessInstanceDetail({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(getProcessInstanceDetail, payload);
      yield put({
        type: "set/ProcessInstanceDetailData",
        payload: datas,
        InstanceDetailCount: totalCount,
      });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * getTaskTotal({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(getTaskTotal, payload);
      yield put({ type: "set/TaskTotalData", payload: datas, TaskTotalCount: totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    * getProcessInstanceTotal({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas = [], totalCount } = yield call(getProcessInstanceTotal, payload);
      yield put({
        type: "set/ProcessInstanceTotalData",
        payload: datas,
        InstanceTotalCount: totalCount,
      });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
  },
  reducers: {
    "set/TaskDetailData": (state, { payload, TaskDetailCount }) => ({
      ...state,
      TaskDetailData: payload,
      TaskDetailCount,
    }),
    "set/ProcessInstanceDetailData": (state, { payload, InstanceDetailCount }) => ({
      ...state,
      ProcessInstanceDetailData: payload,
      InstanceDetailCount,
    }),
    "set/TaskTotalData": (state, { payload, TaskTotalCount }) => ({
      ...state,
      TaskTotalData: payload,
      TaskTotalCount,
    }),
    "set/ProcessInstanceTotalData": (state, { payload, InstanceTotalCount }) => ({
      ...state,
      ProcessInstanceTotalData: payload,
      InstanceTotalCount,
    }),
    clearData: () => ({ ...initData }),
  },
};
